/**
* stepWizard : a jQuery Plugin
* @author : Eugene ONeill
* @url : http://lucidintegration.org
* @documentation : http://lucidintegration.org
* @published : 
* @version : 0.9.1
*/
;(function($) {
	$.fn.stepWizard = function(settings) {
		settings = $.extend({
			duration: 'medium',
			height : 300,
			width : 920,
			disabled: [],
			hidebutton : false,
			next : 'Next',
			prev : 'Back',
			clickthrough : true,
			jump : true,
			header: 'h3',
			separator: ' | '
		}, settings);
		
		var currentStep = 0;
		var obj = $(this);
		
		var relsettings = (obj.attr("rel") || "").split(",");
		
		$.each(relsettings, function(i, item) {
			var s = item.split(":");
			if(typeof settings[s[0]] != "undefined" && s.length == 2)
				settings[s[0]] = s[1];
			return true;
		});
		
		obj.css('width',settings.width+'px').wrapInner('<div class="wizardContent" />');
		$('.wizardContent',obj).css('height',settings.height+'px').after('<div class="buttons"><button type="button" class="previous">'+settings.prev+'</button><button type="button" class="next">'+settings.next+'</button></div>');
		var navSteps = '';
		var h = $('.wizardContent',obj).height();
		$('.wizardContent > div',obj).each(function(i){
			$(this).addClass('stepPanel '+i).hide().css('height',h+'px');
			var stepData = $(settings.header,$(this)).text().split(settings.separator);
			$(settings.header,$(this)).text(stepData[0]);
			if(stepData[1] == null)
				stepData[1] = '';
			navSteps += '<li><a title="+stepData[1]+"><strong>'+stepData[0]+'</strong>'+stepData[1]+'</a></li>';
		});
		
		obj.append('<ul class="navSteps">'+navSteps+'</ul>');
		var _stepList = $('.navSteps li',obj);
		var _states = new Array();
		_stepList.each(function(i){
			if($.inArray(i,settings.disabled)==-1)
				_states.push(i);
			else
				$(this).hide();
		});
		
		var _prev = $('.previous',obj);
		var _next = $('.next',obj);
		var _content = $('.wizardContent',obj);
		var _prevPanel = null;
		var _currentPanel = null;
	
		var totalSteps = _states.length;
		var n = _states[totalSteps-1] + 1;
		$('.navSteps li:nth-child('+n+')',obj).addClass('last');
		
		var cursor = (settings.clickthrough) ? 'pointer' : 'default';
		var w = Math.floor(settings.width/totalSteps);
		var wlast = (settings.width)-w*(totalSteps-1);
		_stepList.each(function(i){
			$(this).data('i',i);
			var wcurr = (_states[totalSteps-1]==i) ? wlast : w;
			$(this).css('width',wcurr+'px').css('cursor',cursor);
		});
	
		
		function updateStep(){
			if(currentStep < 0) currentStep = 0;
			else if(currentStep >= totalSteps) currentStep = totalSteps - 1;
			
			if(settings.hidebutton)
			{
				if(currentStep==0) _prev.hide();
				else _prev.show();
				
				if(currentStep==totalSteps-1) _next.hide();
				else _next.show();
			}
			else
			{
				_prev.attr('disabled',(currentStep==0));
				_next.attr('disabled',(currentStep==totalSteps-1));
			}
			
			_currentPanel = $('.stepPanel.'+_states[currentStep],obj);
			
			if(_prevPanel == null)
				_currentPanel.show();
			else if(!_prevPanel.is('.stepPanel.'+_states[currentStep]))
				_prevPanel.hide(1,function(){
					_currentPanel.fadeIn(settings.duration);
				});
			
			_prevPanel = _currentPanel;
			
			_stepList.each(function(i){
				$(this).removeClass('current done lastDone');
				if(i == _states[currentStep]) $(this).addClass('current');
				else if(i == _states[currentStep - 1]) $(this).addClass('lastDone');
				else if(i < _states[currentStep]) $(this).addClass('done');
			});
		};
		
		updateStep();
		_prev.click(function(){
			updateStep(--currentStep);
		});
		_next.click(function(){
			updateStep(++currentStep);
		});
		
		if(settings.clickthrough){
			_stepList.click(function(){
				if($(this).data('i') == _states[currentStep+1])
					updateStep(++currentStep);
				else if(($(this).attr('class').search(/done/i)!=-1) || (settings.jump))	// click to next step, or jump to any step if allowed
					updateStep(currentStep=$(this).data('i'));
			});
		}
		return this;
	};
})(jQuery);